package phapp.service.luckstar.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.page.PageRequest;
import javacommon.base.*;
import phapp.model.custom.Custom;
import phapp.model.luckstar.*;
import phapp.model.qfd.Custqfd;
import phapp.dao.luckstar.*;
import phapp.service.common.QfdException;
import phapp.service.custom.CustomManager;
import phapp.service.luckstar.impl.*;
import phapp.service.luckstar.*;

/**
 * Title: t_stardeal_data 业务处理层 Description: spring事务实现层,调用Dao层取数据 Copyright:
 * Copyright (c) 2012-10 Company:
 * 
 * @author you name
 * @version 1.0
 */

@Component("stardealManager")
@Transactional
public class StardealManagerImpl extends BaseManager<Stardeal, java.lang.Integer> implements StardealManager
{
   public static final int STAR_DEAL_TYPE_GIFT = 0;
   public static final int STAR_DEAL_TYPE_BUY = 1;
   
   private StardealDao stardealDao;
   private CustomManager customManager;

   /** 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性 */
   public void setStardealDao(StardealDao dao)
   {
      this.stardealDao = dao;
   }

   /** 获取Dao */
   public EntityDao getEntityDao()
   {
      return this.stardealDao;
   }

   public void setCustomManager(CustomManager customManager)
   {
      this.customManager = customManager;
   }

   /**
    * <p>
    * 分页查询Stardeal<br>
    * 
    * @param PageRequest
    *           pr
    * @return Page
    * @author
    */
   @Transactional(readOnly = true)
   public Page findByPageRequest(PageRequest pr)
   {
      return stardealDao.findByPageRequest(pr);
   }

   /**
    * <p>
    * 批量删除 Stardeal<br>
    * 
    * @param Stardeal
    *           u
    * @return Boolean
    * @author
    */
   @Transactional(readOnly = true)
   public Boolean removeByIdArry(Stardeal u)
   {
      Boolean result = false;
      int i = -1, size = u.getIdList().size();
      i = stardealDao.removeByIdArry(u);
      if (i == size)
      {
         result = true;
      }
      else
      {
         throw new RuntimeException();
      }
      return result;
   }

   public Map buyStar(JSONObject jsonData)
   {
      int nStatus = 0;
      Integer ReceiverID;
      Stardeal starData = null;
      Custom customData = null;
      Custom receiverData = null;
      List listQuery = null;
      String CustAccount = "", RecvAccount = "";
      Map<String, Object> mapResult = new HashMap<String, Object>();
      Map<String, Object> mapQuery = new HashMap<String, Object>();
      
      try
      {
         CustAccount = jsonData.getString("user");
         RecvAccount = jsonData.getString("receiver");
         if(RecvAccount == null || RecvAccount.length() == 0)
            RecvAccount = CustAccount;
         
         mapQuery.clear();
         mapQuery.put("custAccount", RecvAccount);
         listQuery = customManager.findList(mapQuery);
         if(listQuery.size() == 0 || RecvAccount == null || RecvAccount.length() == 0)
            //接收者不存在
            throw new QfdException(3001);
         if(listQuery.size() > 1)
            //存在多个
            throw new QfdException(3003);
         
         receiverData = (Custom) listQuery.get(0);
         ReceiverID = receiverData.getCustId();
         
         mapQuery.clear();
         mapQuery.put("custAccount", CustAccount);
         listQuery = customManager.findList(mapQuery);
         if(listQuery.size() == 0 || CustAccount == null || CustAccount.length() == 0)
            //客户不存在
            throw new QfdException(2000);
         
         customData = (Custom) listQuery.get(0);

         starData = new Stardeal();
         starData.setCustId(customData.getCustId());
         //starData.setReceierId(ReceiverID);
         starData.setOrderCount(jsonData.getInt("starcount"));
         starData.setOrderMoney(jsonData.getInt("paymoney"));
         starData.setOrderType(STAR_DEAL_TYPE_BUY);
         save(starData);
         
         //更新luckstar数量
         receiverData.setTotalStar(receiverData.getTotalStar() + jsonData.getInt("starcount"));
         customManager.update(receiverData);
         nStatus = 1000;
      }
      catch(QfdException eQfd)
      {
         nStatus = eQfd.getErrorCode();
      }
      catch(Exception ePray)
      {
         nStatus = 10000;
         ePray.printStackTrace();
      }
      finally
      {
         mapResult.put("statusCode", nStatus);
         if(nStatus == 1000)
            mapResult.put("message", "购买成功！");
         else if(nStatus == 2000)
            mapResult.put("message", "用户不存在！");
         else if(nStatus == 3001)
            mapResult.put("message", "接收者不存在！");
         else if(nStatus == 3003)
            mapResult.put("message", "存在多个接收者！");
         else if(nStatus == 2003)
            mapResult.put("message", "密码错误！");
         else
            mapResult.put("message", "购买失败！");
      }
      
      return mapResult;
   }

   public Map getStarDeal(JSONObject jsonData)
   {
      int nStatus = 0, i = 0;
      Integer ReceiverID;
      Stardeal starData = null;
      Custom customData = null;
      Custom receiverData = null;
      List listQuery = null;
      JSONArray arrayData = null;
      JSONObject jsonStardeal = null;
      String CustAccount = "", RecvAccount = "";
      Map<String, Object> mapResult = new HashMap<String, Object>();
      Map<String, Object> mapQuery = new HashMap<String, Object>();
      
      try
      {
         CustAccount = jsonData.getString("user");
            
         mapQuery.clear();
         mapQuery.put("custAccount", CustAccount);
         listQuery = customManager.findList(mapQuery);
         if(listQuery.size() == 0 || CustAccount == null || CustAccount.length() == 0)
         {
            nStatus = 2000;
         }
         else
         {
            customData = (Custom) listQuery.get(0);
            mapQuery.clear();
            //mapQuery.put("custId", customData.getCustId());
            mapQuery.put("receierId", customData.getCustId());
            listQuery = stardealDao.findList(mapQuery);
            
            arrayData = new JSONArray();
            for(i=0; i<listQuery.size(); i++)
            {
               starData = (Stardeal) listQuery.get(i);
               jsonStardeal = new JSONObject();
               jsonStardeal.put("orderid", starData.getOrderId());
               jsonStardeal.put("custid", starData.getCustId());
               //jsonStardeal.put("receiver", starData.getReceierId());
               jsonStardeal.put("ordertype", starData.getOrderType());
               jsonStardeal.put("starcount", starData.getOrderCount());
               jsonStardeal.put("paymoney", starData.getOrderMoney());
               jsonStardeal.put("orderdate", starData.getOrderDateString());
               
               arrayData.add(jsonStardeal);
               jsonStardeal = null;
            }
            
            mapResult.put("stardeallist", arrayData);
            nStatus = 1000;
         }
      }
      catch(Exception ePray)
      {
         nStatus = 10000;
         ePray.printStackTrace();
      }
      finally
      {
         mapResult.put("statusCode", nStatus);
         if(nStatus == 1000)
            mapResult.put("message", "操作成功！");
         else if(nStatus == 2000)
            mapResult.put("message", "用户不存在！");
         else if(nStatus == 3001)
            mapResult.put("message", "接收者不存在！");
         else if(nStatus == 3003)
            mapResult.put("message", "存在多个接收者！");
         else if(nStatus == 2003)
            mapResult.put("message", "密码错误！");
         else
            mapResult.put("message", "操作失败！");
      }
      
      return mapResult;
   }
   
   
}
